import {assign, isArray} from "min-dash";
import {is} from "bpmn-js/lib/util/ModelUtil";
import {isAny} from "bpmn-js/lib/features/modeling/util/ModelingUtil";
import {getChildLanes} from "bpmn-js/lib/features/modeling/util/LaneUtil";
import {isExpanded} from "bpmn-js/lib/util/DiUtil";


export default function ContextPadProvider(
    config,
    injector,
    contextPad,
    modeling,
    elementFactory,
    connect,
    create,
    canvas,
    popupMenu,
    rules,
    translate
) {
    this.modeling = modeling;
    this.elementFactory = elementFactory;
    this.connect = connect;
    this.create = create;
    this.canvas = canvas;
    this.popupMenu = popupMenu;
    this.contextPad = contextPad;
    this.rules = rules;
    this.translate = translate;
    config = config || {};

    if (config.autoPlace !== false) {
        this.autoPlace = injector.get("autoPlace", false);
    }

    contextPad.registerProvider(this);
}

ContextPadProvider.$inject = [
    "config.contextPad",
    "injector",
    "contextPad",
    "modeling",
    "elementFactory",
    "connect",
    "create",
    "canvas",
    "popupMenu",
    "rules",
    "translate"
];

ContextPadProvider.prototype.getContextPadEntries = function (element) {
    const {
        autoPlace,
        create,
        canvas,
        popupMenu,
        contextPad,
        elementFactory,
        translate,
        modeling,
        rules,
        connect
    } = this;

    var businessObject = element.businessObject;

    // 设置颜色图标
    const setColorIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22"><path d="m12.5 5.5.3-.4 3.6-3.6c.5-.5 1.3-.5 1.7 0l1 1c.5.4.5 1.2 0 1.7l-3.6 3.6-.4.2v.2c0 1.4.6 2 1 2.7v.6l-1.7 1.6c-.2.2-.4.2-.6 0L7.3 6.6a.4.4 0 0 1 0-.6l.3-.3.5-.5.8-.8c.2-.2.4-.1.6 0 .9.5 1.5 1.1 3 1.1zm-9.9 6 4.2-4.2 6.3 6.3-4.2 4.2c-.3.3-.9.3-1.2 0l-.8-.8-.9-.8-2.3-2.9" /></svg>`

    function removeElement() {
        modeling.removeElements([element]);
    }

    function startConnect(event, element) {
        connect.start(event, element);
    }

    function splitLaneHandler(count) {
        return function(event, element) {
            // actual split
            modeling.splitLane(element, count);

            // refresh context pad after split to
            // get rid of split icons
            contextPad.open(element, true);
        };
    }

    function appendAction(type, className, title, options) {
        function appendStart(event, element) {
            var shape = elementFactory.createShape(assign({ type: type }, options));
            create.start(event, shape, {
                source: element
            });
        }

        var append = autoPlace
            ? function (event, element) {
                var shape = elementFactory.createShape(
                    assign({ type: type }, options)
                );
                autoPlace.append(element, shape);
            }
            : appendStart;

        return {
            group: "model",
            className: className,
            title: title,
            action: {
                dragstart: appendStart,
                click: append
            }
        };
    }


    let actions = {};
    // label
    if (element.type === "label") {
        return actions;
    }

    // 创建池/参与者
    if (isAny(businessObject, ["bpmn:Lane", "bpmn:Participant"]) && isExpanded(businessObject)) {
        let childLanes = getChildLanes(element);

        assign(actions, {
            "lane-insert-above": {
                group: "lane-insert-above",
                className: "bpmn-icon-lane-insert-above",
                title: translate("Add Lane above"),
                action: {
                    click: function(event, element) {
                        modeling.addLane(element, "top");
                    }
                }
            }
        });

        if (childLanes.length < 2) {
            if (element.height >= 120) {
                assign(actions, {
                    "lane-divide-two": {
                        group: "lane-divide",
                        className: "bpmn-icon-lane-divide-two",
                        title: translate("Divide into two Lanes"),
                        action: {
                            click: splitLaneHandler(2)
                        }
                    }
                });
            }

            if (element.height >= 180) {
                assign(actions, {
                    "lane-divide-three": {
                        group: "lane-divide",
                        className: "bpmn-icon-lane-divide-three",
                        title: translate("Divide into three Lanes"),
                        action: {
                            click: splitLaneHandler(3)
                        }
                    }
                });
            }
        }
        assign(actions, {
            "lane-insert-below": {
                group: "lane-insert-below",
                className: "bpmn-icon-lane-insert-below",
                title: translate("Add Lane below"),
                action: {
                    click: function(event, element) {
                        modeling.addLane(element, "bottom");
                    }
                }
            }
        });
    }
    else if (isAny(businessObject, ["bpmn:SequenceFlow", "bpmn:TextAnnotation", "bpmn:Group", "bpmn:EndEvent"])) {
        // null

    } else {
        assign(actions, {
            "append.end-event": appendAction("bpmn:EndEvent", "bpmn-icon-end-event-none", translate("Append EndEvent")),
            "append.gateway": appendAction("bpmn:ExclusiveGateway", "bpmn-icon-gateway-none", translate("Append Gateway")),
            "append.append-task": appendAction("bpmn:UserTask", "bpmn-icon-user-task", translate("Append Task")),
            "append.intermediate-event": appendAction(
                "bpmn:IntermediateThrowEvent",
                "bpmn-icon-intermediate-event-none",
                translate("Append Intermediate/Boundary Event")
            )
        });
    }

    // 文本注释
    if (!is(businessObject, "bpmn:TextAnnotation")) {
        assign(actions, {
            "append.text-annotation": appendAction("bpmn:TextAnnotation", "bpmn-icon-text-annotation", translate("Append TextAnnotation"))
        });
    }

    // 修改类型
    if (!popupMenu.isEmpty(element, "bpmn-replace")) {
        // Replace menu entry
        assign(actions, {
            replace: {
                group: "edit",
                className: "bpmn-icon-screw-wrench",
                title: translate("Change type"),
                action: {
                    click: function(event, element) {
                        const canvasRect = contextPad.getPad(element).html.getBoundingClientRect();
                        let position = {x: canvasRect.left, y: canvasRect.bottom + 5}
                        popupMenu.open(element, "bpmn-replace", position, {
                            title: translate("Change type"),
                            width: 300,
                            search: !0
                        });
                    }
                }
            }
        });
    }

    // 修改颜色
    assign(actions, {
        "set-color": {
            group: "edit",
            html: `<div class="entry">${setColorIcon}</div>`,
            title: translate("Set Color"),
            action: {
                click: function(event, element) {
                    let position = assign(() => {
                        const contextPadHtml = contextPad.getPad(element).html;
                        const contextPadRect = contextPadHtml.getBoundingClientRect();
                        const canvasRect = canvas.getContainer().getBoundingClientRect();
                        const top = contextPadRect.top - canvasRect.top;
                        const left = contextPadRect.left - canvasRect.left + contextPadRect.height;
                        console.log(contextPadRect.height);
                        return {x: left, y: top + contextPadRect.height + 5}
                    }, {
                        cursor: {x: event.x, y: event.y}
                    });
                    popupMenu.open(element, "color-picker", position);
                }
            }
        }
    });

    // 连接线
    if(!isAny(businessObject, ["bpmn:SequenceFlow", "bpmn:TextAnnotation", "bpmn:Group"])){
        // 连接线
        assign(actions, {
            connect: {
                group: "connect",
                className: "bpmn-icon-connection-multi",
                title: translate("Connect using DataInputAssociation"),
                action: {
                    click: startConnect,
                    dragstart: startConnect
                }
            }
        });
    }

    // 删除
    // delete element entry, only show if allowed by rules
    var deleteAllowed = rules.allowed("elements.delete", { elements: [element] });

    if (isArray(deleteAllowed)) {
        // was the element returned as a deletion candidate?
        deleteAllowed = deleteAllowed[0] === element;
    }

    if (deleteAllowed) {
        assign(actions, {
            delete: {
                group: "edit",
                className: "bpmn-icon-trash",
                title: translate("Remove"),
                action: {
                    click: removeElement
                }
            }
        });
    }
    return actions;
};
